﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;

namespace ChessServises
{
    [Serializable]
    public class ChessBoard : ISerializable
    {
        public List<Figure> Figures { get; set; }

        public ChessBoard(SerializationInfo info, StreamingContext context)
        {
            Figures = info.GetValue("Table", typeof(List<Figure>)) as List<Figure>;
        }

        public ChessBoard()
        {
            Figures = new List<Figure>();
            InitializeTable();
        }

        private void InitializeTable()
        {
            AddPawns();
            AddKingsAndQueens();
            AddRooks();
            AddBishops();
            AddKnights();
        }

        public void Move(ChessFigure figure, Colour colour, int newXCoordinate, int newYCoordinate)
        {
            Figure tableFigure = FindFigure(figure, colour);
            tableFigure.XCoordinate = newXCoordinate;
            tableFigure.YCoordinate = newYCoordinate;
        }

        private Figure FindFigure(ChessFigure figure, Colour colour)
        {
            foreach (Figure tableFigure in Figures)
            {
                if (tableFigure.ChessPiece == figure && tableFigure.FigureColour == colour)
                {
                    return tableFigure;
                }
            }
            return null;
        }

        private void AddKnights()
        {
            Figure leftWhiteKnight = new Figure(ChessFigure.Knight, 0, 1, Colour.White);
            Figure rightWhiteKnight = new Figure(ChessFigure.Knight, 0, 6, Colour.White);
            Figures.Add(leftWhiteKnight);
            Figures.Add(rightWhiteKnight);

            Figure leftBlackKnight = new Figure(ChessFigure.Knight, 7, 1, Colour.Black);
            Figure rightBlackKnight = new Figure(ChessFigure.Knight, 7, 6, Colour.Black);
            Figures.Add(leftBlackKnight);
            Figures.Add(rightBlackKnight);
        }

        private void AddBishops()
        {
            Figure leftWhiteBishop = new Figure(ChessFigure.Bishop, 0, 2, Colour.White);
            Figure rightWhiteBishop = new Figure(ChessFigure.Bishop, 0, 5, Colour.White);
            Figures.Add(leftWhiteBishop);
            Figures.Add(rightWhiteBishop);

            Figure leftBlackBishop = new Figure(ChessFigure.Bishop, 7, 2, Colour.Black);
            Figure rightBlackBishop = new Figure(ChessFigure.Bishop, 7, 5, Colour.Black);
            Figures.Add(leftBlackBishop);
            Figures.Add(rightBlackBishop);
        }

        private void AddRooks()
        {
            Figure leftWhiteRook = new Figure(ChessFigure.Rook, 0, 0, Colour.White);
            Figure rightWhiteRook = new Figure(ChessFigure.Rook, 0, 7, Colour.White);
            Figures.Add(leftWhiteRook);
            Figures.Add(rightWhiteRook);

            Figure leftBlackRook = new Figure(ChessFigure.Rook, 7, 0, Colour.Black);
            Figure rightBlackRook = new Figure(ChessFigure.Rook, 7, 7, Colour.Black);
            Figures.Add(leftBlackRook);
            Figures.Add(rightBlackRook);
        }

        private void AddPawns()
        {
            for (int i = 0; i < 8; i++)
            {
                Figures.Add(new Figure(ChessFigure.Pawn, i, 1, Colour.White));
                Figures.Add(new Figure(ChessFigure.Pawn, i, 6, Colour.Black));
            }
        }

        private void AddKingsAndQueens()
        {
            Figure whiteKing = new Figure(ChessFigure.King, 4, 0, Colour.White);
            Figure blackKing = new Figure(ChessFigure.King, 4, 7, Colour.Black);
            Figures.Add(whiteKing);
            Figures.Add(blackKing);

            Figure whiteQueen = new Figure(ChessFigure.Queen, 0, 3, Colour.White);
            Figure blackQueen = new Figure(ChessFigure.Queen, 7, 3, Colour.Black);
            Figures.Add(whiteQueen);
            Figures.Add(blackQueen);
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Table", Figures);
        }
    }

    public enum ChessFigure
    {
        Pawn,
        Bishop,
        Knight,
        King,
        Queen,
        Rook
    }

    public enum Colour
    {
        White,
        Black
    }
}